﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using UcAsp.Net.Interface;
using System.Text;
namespace UcAsp.Net.BLL
{
    public class CssConfig
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        public CssConfig()
        {
            if (path.Substring(path.Length - 1, 1) != "\\")
            {
                path = path + "\\";
            }
        }
        public string GetCss(string theme, string cssname)
        {
            if (File.Exists(path + "\\themes\\" + theme + "\\Resources\\css\\defined\\" + cssname + ".css"))
            {
                return File.ReadAllText(path + "\\themes\\" + theme + "\\Resources\\css\\defined\\" + cssname + ".css");
            }
            else
            {
               FileStream fs= File.Create(path + "\\themes\\" + theme + "\\Resources\\css\\defined\\" + cssname + ".css");
               fs.Close();
               fs.Dispose();
                return "";  
            }
        }
        public bool WriteCss(string theme, string cssname, string content)
        {
            if (File.Exists(path + "\\themes\\" + theme + "\\Resources\\css\\defined\\" + cssname + ".css"))
            {

                File.Delete(path + "\\themes\\" + theme + "\\Resources\\css\\defined\\" + cssname + ".css");
                using (FileStream sw = File.Create(path + "\\themes\\" + theme + "\\Resources\\css\\defined\\" + cssname + ".css"))
                {
                    byte[] byt = ASCIIEncoding.UTF8.GetBytes(content.Replace("\n","\r\n"));
                    sw.Write(byt, 0, byt.Count());
                    return true;
                }
            }
            else
            {
                using (FileStream sw = File.Create(path + "\\themes\\" + theme + "\\Resources\\css\\defined\\" + cssname + ".css"))
                {
                    byte[] byt = ASCIIEncoding.UTF8.GetBytes(content.Replace("\n", "\r\n"));
                    sw.Write(byt, 0, byt.Count());
                    return true;
                }
                
            }
        }
        public string[] GetCssList(string theme)
        {
            
            DirectoryInfo info = new DirectoryInfo(path + "\\themes\\" + theme + "\\Resources\\css\\defined\\");
            FileInfo[] file = info.GetFiles();
            string[] dir = new string[file.Length];
            for (int i = 0; i < file.Length; i++)
            {
                dir[i] = file[i].Name.Replace(file[i].Extension, "");
            }
            return dir;
        }

    }
}
